﻿using System;
namespace Kis.Xroad.Eu {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="BasicArray", Namespace="http://kis.x-road.eu/", Target=typeof(BasicArray))]
    public class BasicArraySerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected BasicArray clazz;
        
        public BasicArraySerializer(BasicArray clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("BuiltInReferenced2");
            if (clazz.BuiltInReferenced2!=null) {
                if (((clazz.BuiltInReferenced2 == null) 
                            || (clazz.BuiltInReferenced2.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                }
                for (int x = 0; ((clazz.BuiltInReferenced2 != null) 
                            && (x < clazz.BuiltInReferenced2.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("item");
                    if (clazz.BuiltInReferenced2[x]!=null) {
                        Writer.WriteString(new global::Xtee.Core.Schema.XsdDecimal(((decimal)(clazz.BuiltInReferenced2[x]))).ToString());
                    }
                    else {
                        Writer.WriteAttributeString("xsi", "nil", null, "1");
                    }
                    Writer.WriteEndElement();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : BuiltInReferenced2");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="BuiltInReferenced2") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>BasicArray>BuiltInReferenced2");
                }
                clazz.BuiltInReferenced2 = new System.Collections.Generic.List<System.Decimal?>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("BuiltInReferenced2");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if (Reader.LocalName=="item") {
                            if ((Reader.IsElementNil() == false)) {
                                clazz.BuiltInReferenced2.Add(new global::Xtee.Core.Schema.XsdDecimal(Reader.ReadElementString("item")).Value);
                            }
                            else {
                                clazz.BuiltInReferenced2.Add(null);
                                Reader.Skip();
                            }
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>BasicArray>BuiltInReferenced2>item", Reader.LocalName);
                        }
                    }
                    if ((clazz.BuiltInReferenced2.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>BasicArray>BuiltInReferenced2", Reader.LocalName);
            }
        }
    }
}
